package com.turing.post.cardmanagement.impl;

import java.math.BigInteger;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.cardmanagement.util.MakeCardActionDetail;
import com.turing.post.cardmanagement.util.MakeCardActionDetailDao;
import com.turing.post.util.Config;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;

/**
 * 制卡申请状态迁移明细
 * 
 * @author YangShuo
 * 
 */
@Stateless(mappedName = "cardmanagement.impl.MakeCardActionDetailDaoImpl")
@Remote
@Local
public class MakeCardActionDetailDaoImpl implements MakeCardActionDetailDao {

	/**
	 * 实体管理器
	 * 
	 * @see javax.persistence.EntityManager
	 */
	@PersistenceContext
	protected EntityManager _em;

	@SuppressWarnings("unchecked")
	@Override
	public List<MakeCardActionDetail> getMakeCardActionDetailByMakeCardApplyForId(
			BigInteger MakeCardApplyForId) {
		String sql = "select * from MakeCardActionDetail where MakeCardApplyForId="
				+ MakeCardApplyForId+" order by ActionTime";
		Query query = _em.createNativeQuery(sql, MakeCardActionDetail.class);
		return query.getResultList();
	}

	@Override
	public void insertMakeCardActionDetail(MakeCardActionDetail mcad)
			throws PCPSApplicationException {
		GeneralDao.insert(_em, mcad);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MakeCardActionDetail> getMakeCardActionDetailList(
			String StarTime, String EndTime, String tnitialStatus,
			String endStatus, String actionType, String makecardApplyforId,
			int page) {
		String sql = "select * from MakeCardActionDetail where 1=1"
				+ " and (?1 is null or ActionTime>=to_date(?1,'yyyy-mm-dd hh24:mi:ss'))"
				+ " and (?2 is null or ActionTime<=to_date(?2,'yyyy-mm-dd hh24:mi:ss'))"
				+ " and (?3 is null or InitialStatus=?3)"
				+ " and (?4 is null or EndStatus=?4)"
				+ " and (?5 is null or ActionType=?5)"
				+ " and (?6 is null or makecardApplyforId=?6) order by ActionTime desc";
		int maxResult = Config.PAGE_SIZE;
		int startPosition = maxResult * page;
		Query query = _em.createNativeQuery(sql, MakeCardActionDetail.class);
		query.setParameter(1, StarTime);
		query.setParameter(2, EndTime);
		query.setParameter(3, tnitialStatus);
		query.setParameter(4, endStatus);
		query.setParameter(5, actionType);
		query.setParameter(6, makecardApplyforId);
		if (page != -1) {
			query.setFirstResult(startPosition);
			query.setMaxResults(maxResult);
		}
		return query.getResultList();
	}

	@Override
	public Integer getMakeCardActionDetailCount(String StarTime,
			String EndTime, String tnitialStatus, String endStatus,
			String actionType, String makecardApplyforId) {
		String sql = "select count(*) from MakeCardActionDetail where 1=1"
				+ " and (?1 is null or ActionTime>=to_date(?1,'yyyy-mm-dd hh24:mi:ss'))"
				+ " and (?2 is null or ActionTime<=to_date(?2,'yyyy-mm-dd hh24:mi:ss'))"
				+ " and (?3 is null or InitialStatus=?3)"
				+ " and (?4 is null or EndStatus=?4)"
				+ " and (?5 is null or ActionType=?5)"
				+ " and (?6 is null or makecardApplyforId=?6) order by ActionTime desc";
		Query query = _em.createNativeQuery(sql);
		query.setParameter(1, StarTime);
		query.setParameter(2, EndTime);
		query.setParameter(3, tnitialStatus);
		query.setParameter(4, endStatus);
		query.setParameter(5, actionType);
		query.setParameter(6, makecardApplyforId);
		return Integer.parseInt(query.getSingleResult().toString());
	}

	@Override
	public void deleteMakeCardActionDetail(BigInteger MakeCardApplyForId)
			throws PCPSApplicationException {
		String sql = "delete from makecardactiondetail t where t.makecardapplyforid=?1";
		Query query = _em.createNativeQuery(sql);
		query.setParameter(1, MakeCardApplyForId);
		query.executeUpdate();
	}
}
